#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
import json

def register_admin():
    """注册admin用户"""
    url = "http://localhost:8080/api/auth/register"
    data = {
        "username": "admin2",
        "password": "123456",
        "email": "admin2@xhs.com",
        "nickname": "管理员2"
    }
    
    try:
        response = requests.post(url, json=data)
        print(f"admin2注册响应状态: {response.status_code}")
        print(f"admin2注册响应内容: {response.text}")
        
        if response.status_code == 200:
            print("✅ admin2用户注册成功")
            return True
        else:
            print("❌ admin2用户注册失败")
            return False
    except Exception as e:
        print(f"❌ admin2注册请求异常: {str(e)}")
        return False

def test_admin_login():
    """测试admin2登录"""
    url = "http://localhost:8080/api/auth/login" 
    data = {
        "usernameOrEmail": "admin2",
        "password": "123456"
    }
    
    try:
        response = requests.post(url, json=data)
        print(f"admin2登录响应状态: {response.status_code}")
        print(f"admin2登录响应内容: {response.text}")
        
        if response.status_code == 200:
            result = response.json()
            if result.get('success'):
                print("✅ admin2用户登录成功")
                token = result['data']['token']
                print(f"Token: {token[:50]}...")
                return token
        
        print("❌ admin2用户登录失败")
        return None
    except Exception as e:
        print(f"❌ admin2登录请求异常: {str(e)}")
        return None

def test_original_admin():
    """测试原始admin用户的多种密码"""
    url = "http://localhost:8080/api/auth/login" 
    passwords = ["password", "123456", "admin", "password123", "123456789"]
    
    print("测试原始admin用户的不同密码:")
    for pwd in passwords:
        data = {
            "usernameOrEmail": "admin",
            "password": pwd
        }
        
        try:
            response = requests.post(url, json=data)
            print(f"密码 '{pwd}': {response.status_code}")
            if response.status_code == 200:
                result = response.json()
                if result.get('success'):
                    print(f"✅ admin用户密码 '{pwd}' 登录成功!")
                    return pwd
        except Exception as e:
            print(f"密码 '{pwd}': 异常 - {str(e)}")
    
    print("❌ 所有测试密码都失败")
    return None

if __name__ == "__main__":
    print("🎯 Admin用户测试")
    print("=" * 50)
    
    # 先测试原始admin用户
    success_pwd = test_original_admin()
    print()
    
    if not success_pwd:
        # 如果原始admin失败，注册新的admin2用户
        print("原始admin用户登录失败，注册新用户...")
        register_admin()
        print()
        
        # 测试admin2登录
        token = test_admin_login()
        if token:
            print(f"\n🎉 可以使用用户名: admin2, 密码: 123456 进行测试")
        else:
            print("\n❌ admin2测试失败")
    else:
        print(f"\n🎉 可以使用用户名: admin, 密码: {success_pwd} 进行测试") 